package com.huiyun.project.im.core.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huiyun.project.im.consts.ImCsSessionConst;
import com.huiyun.project.im.core.mapper.ImCsSessionMapper;
import com.huiyun.project.im.core.service.IImCsSessionService;
import com.huiyun.project.im.entity.ImCsSession;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 会话列表 服务实现类
 * </p>
 *
 * @author DESKTOP-CIBUH3O
 * @since 2020-07-28
 */
@Service
public class ImCsSessionServiceImpl extends ServiceImpl<ImCsSessionMapper, ImCsSession> implements IImCsSessionService {
    /**
     * 检测并返回有效session
     *
     * @param imCsTenantId
     * @param sessionId
     * @param orgId
     * @param userId
     * @return
     */
    @Override
    public ImCsSession getValidSession(String imCsTenantId, String sessionId, String orgId, String userId) {
        return this.getOne(Wrappers.<ImCsSession>lambdaQuery()
                .eq(ImCsSession::getCsTenantId, imCsTenantId)
                .eq(StrUtil.isNotBlank(sessionId), ImCsSession::getId, sessionId)
                .eq(ImCsSession::getCsOrgId, orgId)
                .eq(ImCsSession::getUserId, userId)
                .ne(ImCsSession::getStatus, ImCsSessionConst.STATUS.END.getValue())
        );
    }

    /**
     * 分页查询客户会话历史
     * 按租户分组
     *
     * @param page
     * @param map
     * @return
     */
    @Override
    public Page<ImCsSession> getCustomSessionList(Page page, Map<String, Object> map) {
        return baseMapper.getCustomSessionList(page, map);
    }
}
